﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class GradoRepository:MasterRepository, IGradoRepository
    {
        public List<GRADO> GetAllFromGrados()
        {
            var query = from p in context.GRADOes
                        select p;

            return query.ToList();
        }

        public List<GRADO> GetAllfromGradosByDescripcion(string criterio)
        {
            if (criterio != string.Empty)
            {
                var query = from p in context.GRADOes
                            where p.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select p;
                return query.ToList();
            }
            else
            {
                var query = from p in context.GRADOes
                            select p;

                return query.ToList();
            }
        }

        public GRADO GetFromGradosById(int id)
        {
            var query = from p in context.GRADOes
                        where p.IdGrado.Equals(id)
                        select p;

            return query.SingleOrDefault();
        }

        public void AddGrados(GRADO grado)
        {
            context.GRADOes.Add(grado);
            context.SaveChanges();
        }

        public void UpdateGrados(GRADO grado)
        {
            context.Entry(grado).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void DeleteGrados(int id)
        {
            var existe = context.GRADOes.Find(id);

            if (existe != null)
            {
                context.GRADOes.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExisteDUplicidad(string criterio, int id)
        {
            var query = from p in context.GRADOes
                        where p.Descripcion.ToUpper().CompareTo(criterio.ToUpper()) == 0 && p.IdGrado != id
                        select p;

            return query.Any();
        }
    }
}
